package com.escape.gateway.exception;

import cn.dev33.satoken.exception.SaTokenException;
import com.escape.gateway.entity.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-08  16:00
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class GatewayExceptionHandler implements ErrorWebExceptionHandler {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
        ServerHttpRequest request = serverWebExchange.getRequest();
        ServerHttpResponse response = serverWebExchange.getResponse();
        Integer code = 200;
        String message = "";

        // 1. 判断异常类型
        if (throwable instanceof SaTokenException) {
            code = 401;
            message = "用户无权限!";
        } else {
            code = 500;
            message = "系统繁忙!";
        }

        // 2. 包装返回类
        Result result = Result.fail(code, message);

        // 3. 设置返回格式为 json
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);

        // 4. 返回
        return response.writeWith(Mono.fromSupplier( () -> {
            DataBufferFactory dataBufferFactory = response.bufferFactory();
            byte[] bytes = null;
            try {
                bytes = objectMapper.writeValueAsBytes(result);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            return dataBufferFactory.wrap(bytes);
        }));
    }
}
